commander_cx16fandomcom-20200215-history
Beep
The very basic requirements to play sound on the YM2151 chip are: # Load a patch into the YM2151 registers for one of the 8 voices (channels). # Set the note value for the channel # Send a KeyDown message to play the note # (wait) # Send a KeyUP message to release the note when it has played for the intended duration When playing multiple notes, if you have not released the previous note (KeyUP) yet, then sending a second KeyDN will NOT play the new note. This would be like trying to press a piano key that you're already holding down. To talk to the YM2151, you POKE information into one of its two I/O memory locations: * Address Register ($9FE0) - tells the YM2151 which of its internal register addresses to use * Data Register ($9FE1) - send the actual values to this register * NOTE: These I/O addresses are very likely to change in the future, as they correspond to expansion port addresses, and if the YM2151 is approved for the system, it is likely to be built into the mainboard directly and will use a different IO address range than the expansion slots. Example program to play two chime sounds in an ascending tone, which might serve as a positive audible feedback tone: 10 S=600 : REM NOTE SPEED 20 YA=$9FE0 : YD=$9FE1 : REM YM2151 ADDRESS/DATA PORTS 30 LN=$3A : HN=$44 : REM LN/HN = LO NOTE / HI NOTE 40 REM --- LOAD PATCH INTO VOICE 0 --- 50 FOR I=1 TO 5 60 READ A : READ D 70 POKE YA,A : POKE YD,D 80 NEXT I 100 REM --- PLAY HAPPY SOUND --- 110 POKE YA,$28 : REM $28 IS VOICE 0 FREQ. SETTING 120 POKE YD,LN : REM SET THE FREQ. TO LO NOTE VALUE 130 POKE YA,$08 : REM SELECT THE KEYUP/DN REGISTER 140 POKE YD,0 : REM RELEASE ANY PREVIOUS NOTE 150 POKE YD,$40 : REM PLAY THE NOTE 160 FOR I = 1 TO S : NEXT 170 POKE YD,0 : REM RELEASE THE NOTE 180 FOR I = 1 TO S : NEXT 190 POKE YA,$28 200 POKE YD,HN : REM SET NEXT NOTE FREQ. TO HI-NOTE 210 POKE YA,$08 220 POKE YD,0 230 POKE YD,$40 240 FOR I = 1 TO S : NEXT 250 POKE YD,0 : REM RELEASE THE SECOND NOTE 260 END 1000 REM --- PATCH DATA --- 1010 DATA $20, $C0 1020 DATA $58, $01 1030 DATA $98, $1F 1040 DATA $B8, $0D 1050 DATA $F8, $F6 The patch data lines are arranged to make it easy to play around with them and see what they do. The settings mean the following: $20 = master channel configuration: $C0 = L & R volume ON, No Feedback, Use OP algorithm 0. $58 = fine detuning / phase multiplier: $01 = no detune (hi nybble) / multiplier=1 (normal) $98 = Key-scale / Attack Rate: $1F = no keyscale / max attack rate (keyscale = 3 MSB, attack = 5 LSB) $B8 = Amplitude Mod ENA / 1st Decay Rate: $0D = no AM / Decay=13 (AM-ENA is 1 MSB / decay is 5 LSB) $F8 = Sustain Level / Release Rate: $F6 = max sustain (0-F in hi nybble / release=6 .. 0-F in lo nybble) Note that this patch is very simple, and only utilizes the 4th operator of voice 0. More advanced patches would require more settings to be made for each voice, which is beyond the scope of a simple "hello world" example for sound.